<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>合同模板管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            overflow: hidden;
        }
        
        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }
        
        .header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
            font-weight: 300;
        }
        
        .header p {
            font-size: 1.1em;
            opacity: 0.9;
        }
        
        .content {
            padding: 40px;
        }
        
        .section {
            margin-bottom: 40px;
            padding: 30px;
            background: #f8f9fa;
            border-radius: 10px;
            border-left: 5px solid #667eea;
        }
        
        .section h2 {
            color: #333;
            margin-bottom: 20px;
            font-size: 1.5em;
            display: flex;
            align-items: center;
        }
        
        .section h2::before {
            content: '📄';
            margin-right: 10px;
            font-size: 1.2em;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-row {
            display: flex;
            gap: 20px;
            margin-bottom: 20px;
        }
        
        .form-col {
            flex: 1;
        }
        
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #555;
        }
        
        input[type="text"], input[type="email"], select, textarea {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e1e5e9;
            border-radius: 8px;
            font-size: 14px;
            transition: all 0.3s ease;
            background: white;
        }
        
        input[type="text"]:focus, input[type="email"]:focus, select:focus, textarea:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }
        
        .btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            padding: 12px 30px;
            border-radius: 25px;
            cursor: pointer;
            font-size: 14px;
            font-weight: 600;
            transition: all 0.3s ease;
            text-decoration: none;
            display: inline-block;
            margin: 5px;
        }
        
        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
        }
        
        .btn-secondary {
            background: linear-gradient(135deg, #6c757d 0%, #495057 100%);
        }
        
        .btn-success {
            background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
        }
        
        .btn-info {
            background: linear-gradient(135deg, #17a2b8 0%, #6f42c1 100%);
        }
        
        .btn-warning {
            background: linear-gradient(135deg, #ffc107 0%, #fd7e14 100%);
            color: #333;
        }
        
        .data-preview {
            background: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 8px;
            padding: 20px;
            margin-top: 20px;
            max-height: 400px;
            overflow-y: auto;
        }
        
        .data-item {
            display: flex;
            justify-content: space-between;
            padding: 8px 0;
            border-bottom: 1px solid #e9ecef;
        }
        
        .data-item:last-child {
            border-bottom: none;
        }
        
        .data-label {
            font-weight: 600;
            color: #495057;
            min-width: 120px;
        }
        
        .data-value {
            color: #6c757d;
            flex: 1;
            text-align: right;
        }
        
        .status-message {
            padding: 15px;
            border-radius: 8px;
            margin: 20px 0;
            display: none;
        }
        
        .status-success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .status-error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        .loading {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 3px solid #f3f3f3;
            border-top: 3px solid #667eea;
            border-radius: 50%;
            animation: spin 1s linear infinite;
            margin-right: 10px;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        .feature-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 20px;
            margin-top: 20px;
        }
        
        .feature-card {
            background: white;
            padding: 25px;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
            text-align: center;
            transition: transform 0.3s ease;
        }
        
        .feature-card:hover {
            transform: translateY(-5px);
        }
        
        .feature-icon {
            font-size: 3em;
            margin-bottom: 15px;
        }
        
        .feature-title {
            font-size: 1.2em;
            font-weight: 600;
            margin-bottom: 10px;
            color: #333;
        }
        
        .feature-desc {
            color: #666;
            line-height: 1.6;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>🏢 合同模板管理系统</h1>
            <p>模板由产品/业务侧维护，开发侧负责数据填充和导出</p>
        </div>
        
        <div class="content">
            <!-- 功能特色展示 -->
            <div class="section">
                <h2>系统特色</h2>
                <div class="feature-grid">
                    <div class="feature-card">
                        <div class="feature-icon">📋</div>
                        <div class="feature-title">模板管理</div>
                        <div class="feature-desc">业务侧维护Word模板，开发侧专注数据处理</div>
                    </div>
                    <div class="feature-card">
                        <div class="feature-icon">🔄</div>
                        <div class="feature-title">数据填充</div>
                        <div class="feature-desc">后端自动获取数据并填充到模板占位符</div>
                    </div>
                    <div class="feature-card">
                        <div class="feature-icon">📤</div>
                        <div class="feature-title">一键导出</div>
                        <div class="feature-desc">支持单个和批量导出Word文档</div>
                    </div>
                    <div class="feature-card">
                        <div class="feature-icon">⚡</div>
                        <div class="feature-title">高效处理</div>
                        <div class="feature-desc">基于Apache POI，处理速度快，支持复杂格式</div>
                    </div>
                </div>
            </div>
            
            <!-- 示例合同生成 -->
            <div class="section">
                <h2>示例合同生成</h2>
                <p style="margin-bottom: 20px; color: #666;">快速生成示例合同，查看模板效果</p>
                <div class="form-row">
                    <button class="btn btn-success" onclick="generateSampleContract()">📄 生成示例合同</button>
                    <button class="btn btn-info" onclick="previewSampleData()">👁️ 预览示例数据</button>
                </div>
                <div id="sampleDataPreview" class="data-preview" style="display: none;"></div>
            </div>
            
            <!-- 员工合同生成 -->
            <div class="section">
                <h2>员工合同生成</h2>
                <p style="margin-bottom: 20px; color: #666;">根据员工ID从后端获取数据并生成合同</p>
                <div class="form-row">
                    <div class="form-col">
                        <label for="employeeId">员工ID：</label>
                        <input type="text" id="employeeId" placeholder="例如：EMP001" value="EMP001">
                    </div>
                    <div class="form-col" style="display: flex; align-items: end; gap: 10px;">
                        <button class="btn" onclick="validateEmployee()">🔍 验证员工</button>
                        <button class="btn btn-success" onclick="generateEmployeeContract()">📄 生成合同</button>
                        <button class="btn btn-info" onclick="previewEmployeeData()">👁️ 预览数据</button>
                    </div>
                </div>
                <div id="employeeValidation" class="status-message"></div>
                <div id="employeeDataPreview" class="data-preview" style="display: none;"></div>
            </div>
            
            <!-- 新员工合同创建 -->
            <div class="section">
                <h2>新员工合同创建</h2>
                <p style="margin-bottom: 20px; color: #666;">为新员工创建合同，系统自动分配其他信息</p>
                <div class="form-row">
                    <div class="form-col">
                        <label for="newEmployeeName">员工姓名：</label>
                        <input type="text" id="newEmployeeName" placeholder="请输入员工姓名" value="李四">
                    </div>
                    <div class="form-col">
                        <label for="newPosition">职位：</label>
                        <select id="newPosition">
                            <option value="Java开发工程师">Java开发工程师</option>
                            <option value="前端开发工程师">前端开发工程师</option>
                            <option value="产品经理">产品经理</option>
                            <option value="UI设计师">UI设计师</option>
                            <option value="测试工程师">测试工程师</option>
                            <option value="运维工程师">运维工程师</option>
                            <option value="数据分析师">数据分析师</option>
                            <option value="项目经理">项目经理</option>
                        </select>
                    </div>
                    <div class="form-col">
                        <label for="newSalary">薪资：</label>
                        <select id="newSalary">
                            <option value="8000元/月">8000元/月</option>
                            <option value="12000元/月">12000元/月</option>
                            <option value="15000元/月" selected>15000元/月</option>
                            <option value="18000元/月">18000元/月</option>
                            <option value="20000元/月">20000元/月</option>
                            <option value="25000元/月">25000元/月</option>
                            <option value="30000元/月">30000元/月</option>
                        </select>
                    </div>
                </div>
                <div class="form-row">
                    <button class="btn btn-success" onclick="createNewContract()">📄 创建新合同</button>
                </div>
            </div>
            
            <!-- 批量数据查看 -->
            <div class="section">
                <h2>批量数据管理</h2>
                <p style="margin-bottom: 20px; color: #666;">查看和管理批量员工数据</p>
                <div class="form-row">
                    <div class="form-col">
                        <label for="batchCount">生成数量：</label>
                        <select id="batchCount">
                            <option value="5">5个</option>
                            <option value="10" selected>10个</option>
                            <option value="20">20个</option>
                            <option value="50">50个</option>
                        </select>
                    </div>
                    <div class="form-col" style="display: flex; align-items: end; gap: 10px;">
                        <button class="btn btn-info" onclick="getBatchData()">📊 获取批量数据</button>
                        <button class="btn btn-warning" onclick="clearBatchData()">🗑️ 清空数据</button>
                    </div>
                </div>
                <div id="batchDataPreview" class="data-preview" style="display: none;"></div>
            </div>
            
            <!-- 状态消息 -->
            <div id="statusMessage" class="status-message"></div>
        </div>
    </div>
    
    <script>
        // 显示状态消息
        function showStatus(message, type = 'success') {
            const statusDiv = document.getElementById('statusMessage');
            statusDiv.className = `status-message status-${type}`;
            statusDiv.textContent = message;
            statusDiv.style.display = 'block';
            
            setTimeout(() => {
                statusDiv.style.display = 'none';
            }, 3000);
        }
        
        // 显示加载状态
        function showLoading(buttonElement, originalText) {
            buttonElement.innerHTML = '<span class="loading"></span>' + '处理中...';
            buttonElement.disabled = true;
            
            return () => {
                buttonElement.innerHTML = originalText;
                buttonElement.disabled = false;
            };
        }
        
        // 生成示例合同
        function generateSampleContract() {
            const btn = event.target;
            const hideLoading = showLoading(btn, '📄 生成示例合同');
            
            fetch('/api/template/contract/sample')
                .then(response => {
                    if (response.ok) {
                        return response.blob();
                    }
                    throw new Error('生成失败');
                })
                .then(blob => {
                    const url = window.URL.createObjectURL(blob);
                    const a = document.createElement('a');
                    a.href = url;
                    a.download = 'sample_contract.docx';
                    document.body.appendChild(a);
                    a.click();
                    window.URL.revokeObjectURL(url);
                    document.body.removeChild(a);
                    showStatus('示例合同生成成功！');
                })
                .catch(error => {
                    showStatus('生成失败：' + error.message, 'error');
                })
                .finally(() => {
                    hideLoading();
                });
        }
        
        // 预览示例数据
        function previewSampleData() {
            const btn = event.target;
            const hideLoading = showLoading(btn, '👁️ 预览示例数据');
            
            fetch('/api/template/contract/sample-data')
                .then(response => response.json())
                .then(data => {
                    displayDataPreview('sampleDataPreview', data);
                    showStatus('示例数据加载成功！');
                })
                .catch(error => {
                    showStatus('加载失败：' + error.message, 'error');
                })
                .finally(() => {
                    hideLoading();
                });
        }
        
        // 验证员工
        function validateEmployee() {
            const employeeId = document.getElementById('employeeId').value;
            if (!employeeId) {
                showStatus('请输入员工ID', 'error');
                return;
            }
            
            const btn = event.target;
            const hideLoading = showLoading(btn, '🔍 验证员工');
            
            fetch(`/api/template/employee/validate/${employeeId}`)
                .then(response => response.json())
                .then(data => {
                    const validationDiv = document.getElementById('employeeValidation');
                    if (data.exists) {
                        validationDiv.className = 'status-message status-success';
                        validationDiv.textContent = `员工验证成功！姓名：${data.employeeName}，职位：${data.position}`;
                    } else {
                        validationDiv.className = 'status-message status-error';
                        validationDiv.textContent = '员工ID不存在，请检查输入';
                    }
                    validationDiv.style.display = 'block';
                })
                .catch(error => {
                    showStatus('验证失败：' + error.message, 'error');
                })
                .finally(() => {
                    hideLoading();
                });
        }
        
        // 生成员工合同
        function generateEmployeeContract() {
            const employeeId = document.getElementById('employeeId').value;
            if (!employeeId) {
                showStatus('请输入员工ID', 'error');
                return;
            }
            
            const btn = event.target;
            const hideLoading = showLoading(btn, '📄 生成合同');
            
            fetch(`/api/template/contract/employee/${employeeId}`)
                .then(response => {
                    if (response.ok) {
                        return response.blob();
                    }
                    throw new Error('生成失败');
                })
                .then(blob => {
                    const url = window.URL.createObjectURL(blob);
                    const a = document.createElement('a');
                    a.href = url;
                    a.download = `contract_${employeeId}.docx`;
                    document.body.appendChild(a);
                    a.click();
                    window.URL.revokeObjectURL(url);
                    document.body.removeChild(a);
                    showStatus('员工合同生成成功！');
                })
                .catch(error => {
                    showStatus('生成失败：' + error.message, 'error');
                })
                .finally(() => {
                    hideLoading();
                });
        }
        
        // 预览员工数据
        function previewEmployeeData() {
            const employeeId = document.getElementById('employeeId').value;
            if (!employeeId) {
                showStatus('请输入员工ID', 'error');
                return;
            }
            
            const btn = event.target;
            const hideLoading = showLoading(btn, '👁️ 预览数据');
            
            fetch(`/api/template/contract/data/${employeeId}`)
                .then(response => response.json())
                .then(data => {
                    displayDataPreview('employeeDataPreview', data);
                    showStatus('员工数据加载成功！');
                })
                .catch(error => {
                    showStatus('加载失败：' + error.message, 'error');
                })
                .finally(() => {
                    hideLoading();
                });
        }
        
        // 创建新合同
        function createNewContract() {
            const employeeName = document.getElementById('newEmployeeName').value;
            const position = document.getElementById('newPosition').value;
            const salary = document.getElementById('newSalary').value;
            
            if (!employeeName) {
                showStatus('请输入员工姓名', 'error');
                return;
            }
            
            const btn = event.target;
            const hideLoading = showLoading(btn, '📄 创建新合同');
            
            const formData = new FormData();
            formData.append('employeeName', employeeName);
            formData.append('position', position);
            formData.append('salary', salary);
            
            fetch('/api/template/contract/create', {
                method: 'POST',
                body: formData
            })
            .then(response => {
                if (response.ok) {
                    return response.blob();
                }
                throw new Error('创建失败');
            })
            .then(blob => {
                const url = window.URL.createObjectURL(blob);
                const a = document.createElement('a');
                a.href = url;
                a.download = `new_contract_${employeeName}.docx`;
                document.body.appendChild(a);
                a.click();
                window.URL.revokeObjectURL(url);
                document.body.removeChild(a);
                showStatus('新员工合同创建成功！');
            })
            .catch(error => {
                showStatus('创建失败：' + error.message, 'error');
            })
            .finally(() => {
                hideLoading();
            });
        }
        
        // 获取批量数据
        function getBatchData() {
            const count = document.getElementById('batchCount').value;
            const btn = event.target;
            const hideLoading = showLoading(btn, '📊 获取批量数据');
            
            fetch(`/api/template/contract/batch/${count}`)
                .then(response => response.json())
                .then(data => {
                    displayBatchDataPreview('batchDataPreview', data);
                    showStatus(`成功加载${data.length}条数据！`);
                })
                .catch(error => {
                    showStatus('加载失败：' + error.message, 'error');
                })
                .finally(() => {
                    hideLoading();
                });
        }
        
        // 清空批量数据
        function clearBatchData() {
            const previewDiv = document.getElementById('batchDataPreview');
            previewDiv.style.display = 'none';
            previewDiv.innerHTML = '';
            showStatus('数据已清空');
        }
        
        // 显示数据预览
        function displayDataPreview(elementId, data) {
            const previewDiv = document.getElementById(elementId);
            previewDiv.innerHTML = '';
            
            const fields = [
                { key: 'contractNo', label: '合同编号' },
                { key: 'employeeName', label: '员工姓名' },
                { key: 'position', label: '职位' },
                { key: 'salary', label: '薪资' },
                { key: 'startDate', label: '开始日期' },
                { key: 'endDate', label: '结束日期' },
                { key: 'phone', label: '联系电话' },
                { key: 'idCard', label: '身份证号' },
                { key: 'address', label: '家庭住址' },
                { key: 'companyName', label: '公司名称' },
                { key: 'legalRepresentative', label: '法定代表人' },
                { key: 'workLocation', label: '工作地点' },
                { key: 'contractPeriod', label: '合同期限' },
                { key: 'probationPeriod', label: '试用期' },
                { key: 'workingHours', label: '工作时间' },
                { key: 'benefits', label: '福利待遇' },
                { key: 'otherTerms', label: '其他条款' }
            ];
            
            fields.forEach(field => {
                if (data[field.key]) {
                    const item = document.createElement('div');
                    item.className = 'data-item';
                    item.innerHTML = `
                        <span class="data-label">${field.label}:</span>
                        <span class="data-value">${data[field.key]}</span>
                    `;
                    previewDiv.appendChild(item);
                }
            });
            
            previewDiv.style.display = 'block';
        }
        
        // 显示批量数据预览
        function displayBatchDataPreview(elementId, dataList) {
            const previewDiv = document.getElementById(elementId);
            previewDiv.innerHTML = '';
            
            dataList.forEach((data, index) => {
                const section = document.createElement('div');
                section.style.marginBottom = '20px';
                section.style.padding = '15px';
                section.style.border = '1px solid #dee2e6';
                section.style.borderRadius = '5px';
                section.style.background = 'white';
                
                const header = document.createElement('h4');
                header.textContent = `员工 ${index + 1}: ${data.employeeName}`;
                header.style.marginBottom = '10px';
                header.style.color = '#495057';
                section.appendChild(header);
                
                const basicInfo = [
                    { key: 'contractNo', label: '合同编号' },
                    { key: 'position', label: '职位' },
                    { key: 'salary', label: '薪资' },
                    { key: 'startDate', label: '开始日期' },
                    { key: 'phone', label: '联系电话' }
                ];
                
                basicInfo.forEach(field => {
                    if (data[field.key]) {
                        const item = document.createElement('div');
                        item.className = 'data-item';
                        item.innerHTML = `
                            <span class="data-label">${field.label}:</span>
                            <span class="data-value">${data[field.key]}</span>
                        `;
                        section.appendChild(item);
                    }
                });
                
                previewDiv.appendChild(section);
            });
            
            previewDiv.style.display = 'block';
        }
        
        // 页面加载完成后的初始化
        document.addEventListener('DOMContentLoaded', function() {
            showStatus('合同模板管理系统已就绪！');
        });
    </script>
</body>
</html>